<!DOCTYPE html>
<html lang="zh-cn">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>给我留言-David个人博客站点</title>

    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
    <link rel="Shortcut Icon" href="../img/ic.png" />
    <link rel="stylesheet" href="../css/header.css">
    <link rel="stylesheet" href="../css/reset.css">
    <link rel="stylesheet" href="../css/swiper.css">
    <link rel="stylesheet" href="../css/section.css">
    <link rel="stylesheet" href="../css/footer.css">
    <link rel="stylesheet" href="../css/comment.css">
    <style>
        #header,
        #section,
        #footer {
            margin-top: 10px;
            margin-bottom: 10px;
        }
    </style>
</head>

<body>
    <!-- 背景 -->
    <canvas id="canvas"> 啥年代了，还用ie？？？抱歉 不做兼容 </canvas>

    <!-- 头部 -->
    <header class="header" id="header">

        <!-- 顶部logo、标题 -->
        <div class="container" id="container">
            <span class="titleLarge">David</span>
            <span class="titleLittle">赵升阳的个人技术博客</span>
        </div>

        <!-- 头部导航栏 -->
        <div class="nav-header" id="nav-header">
            <ul id="ul">
                <li class="options"><a
                        href="javascript:window.location.href= '../index.html.html'">首&nbsp;&nbsp;&nbsp;&nbsp;页</a>
                </li>
                <li class="options menu">
                    <a href="#">生活笔记</a>
                    <ul class="secondMenu">
                        <li class="secondOptions"><a href="#">个人随笔</a></li>
                        <li class="secondOptions"><a href="#">个人日记</a></li>
                        <li class="secondOptions"><a href="#">个人展示</a></li>
                    </ul>
                </li>
                <li class="options menu">
                    <a href="#">技术杂谈</a>
                    <ul class="secondMenu">
                        <li class="secondOptions"><a>C/C++</a></li>
                        <li class="secondOptions"><a>Java</a></li>
                        <li class="secondOptions"><a>JavaScript</a></li>
                        <li class="secondOptions"><a>python</a></li>
                        <li class="secondOptions"><a>HTML</a></li>
                        <li class="secondOptions"><a>Other</a></li>
                    </ul>
                </li>
                <li class="options"><a href="./welfarePage.html">福利专区</a></li>
                <li class="options"><a href="./introduction.html">关于博主</a></li>
                <li class="options"><a href="./comments.html">给我留言</a></li>
                <li class="options"><a href="./sponsor.html">赞助作者</a></li>
                <li class="options"><a href="./exchange.html">技术交流</a></li>
                <li class="options"><a href="./project.html">项目合作</a></li>
                <li class="search" id="search">search</li>
            </ul>
        </div>
    </header>

    <!-- 主体 -->
    <section id="section" class="section">
        <div id="contain-wrapper">

            <div class="title">给我留言</div>
            <div class="upload">
                <div class="uploadText">发表我的评论
                    <span v-show="reply"> &nbsp回复：{{ reply.nickName }}
                        <span @click="reply = false" style="cursor: pointer;">×</span>
                    </span>
                </div>
                <div class="uploadInfo">
                    <span>请填写你的昵称:</span>
                    <input autofocus type="text" id="username" v-model="username">
                    <span v-show="!username" style="color: rgb(250, 110, 110);font-size:16px">昵称不能为空哦</span>
                </div>
                <div class="input">
                    <textarea autofocus id="input" placeholder="写点什么..." v-model="coText"></textarea>
                </div>
                <button @click="uploadCo()">提交评论</button>
            </div>

            <div class="commentContent">
                <div v-for="comment in comments" class="comment">
                    <div class="coWrapper">
                        <div class="coImg"><img src="../img/head.png" alt=""></div>
                        <div class="Icontent">
                            <div class="coInfo">{{ comment.content }}</div>
                            <div class="coOther">
                                <span>{{ comment.nickName }}</span>
                                <span>{{ comment.time }}</span>
                                <span @click="setCo(comment)">回复</span>
                            </div>
                        </div>
                    </div>
                    <div v-for="co in comment.children" class="childComment">
                        <div class="coWrapper">
                            <div class="coImg"><img src="../img/head.png" alt=""></div>
                            <div class="Icontent">
                                <div class="coInfo">{{ co.content }}</div>
                                <div class="coOther">
                                    <span>{{ co.nickName }}</span>
                                    <span>{{ co.time }}</span>
                                    <span @click="setCo(comment)">回复</span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>


        </div>
    </section>
    <!-- 底部 -->
    <footer>
        <div class="container">
            <div class="info">
                stay hungry stay foolish
            </div>
        </div>
    </footer>
    <!-- 飞升按钮 -->
    <img id="socket" src="../img/socket.png">
    <script src="../js/word.js"></script>
    <script src="../js/swiper.js"></script>
    <script src="../js/header.js"></script>
    <script src="../js/socket.js"></script>
    <!-- Initialize Swiper -->
    <script>

        var app = new Vue({
            el: '#section',
            data: {
                commentList: [],
                comments: [],
                reply: false,
                username: '',
                coText: ''
            },
            mounted: function () {
                this.init()
            },
            methods: {
                init: function () {
                    var that = this;
                    let xhr = new XMLHttpRequest()
                    xhr.open('GET', '../php/getAllComment.php', true)
                    xhr.send();
                    xhr.onreadystatechange = function () {
                        if (xhr.readyState === 4 && xhr.status === 200) {
                            // 获取数据库的数据
                            that.commentList = JSON.parse(xhr.responseText)
                            // 处理数据
                            that.figureOut()
                        }
                    }
                },
                // 处理数据将子评论加到父评论的children数组中
                figureOut: function () {
                    var that = this;
                    this.comments = this.commentList.filter(el => {
                        return el.parentId < 0
                    })
                    this.comments.forEach(el => {
                        el.children = this.commentList.filter(ele => {
                            return el.id == ele.parentId
                        })
                    });
                    console.log(this.comments)
                },
                uploadCo: function () {
                    var that = this
                    if (!this.username) return;
                    if (!this.coText) { alert('你总得评论点东西吧'); return }
                    let xhr = new XMLHttpRequest()
                    xhr.open('POST', '../php/addComment.php', true)
                    xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded')
                    var data = 'username=' + this.username + '&comment=' + this.coText + '&id=' + (this.commentList.length + 1)
                    if (this.reply)
                        data += '&replyId=' + this.reply.id
                    else
                        data += '&replyId=-1'
                    xhr.send(data)
                    xhr.onreadystatechange = function () {
                        if (xhr.readyState === 4 && xhr.status === 200) {
                            that.init()
                            that.coText = ''
                            alert('评论成功')
                            document.documentElement.scrollTop = document.body.clientHeight
                        }
                    }
                },
                setCo: function (comment) {
                    this.reply = comment
                    document.documentElement.scrollTop = 0
                }
            }
        })

    </script>
</body>

</html>